package com.bcqn.service;

import com.bcqn.dao.JobCollectionMapper;
import com.bcqn.dao.JobDeliveryMapper;
import com.bcqn.dao.PositionMapper;
import com.bcqn.model.*;
import com.bcqn.model.vo.JobQueryObj;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作的服务层
 *
 * @author liugang
 * @version 1.0
 * @date 2020/5/2 15:06
 */
@Service
public class JobService {

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private JobCollectionMapper jobCollectionMapper;

    @Resource
    private JobDeliveryMapper jobDeliveryMapper;

    /**
     * 添加求职信息
     *
     * @param position
     * @return
     */
    public Object add(Position position) {
        return positionMapper.insertSelective(position);
    }

    /**
     * 通过职务的id来查询职务的详细信息
     *
     * @param id
     * @return
     */
    public Position detail(Long id) {
        return positionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询工作的对应的分类的信息列表
     *
     * @param jobQueryObj
     * @param pageNO
     * @return
     */
    public PageInfo<Position> getList(
            int pageNum, int pageSize, String jobType, String recruitType, String keyword, String sortKey, String order) {
        PageHelper.startPage(pageNum, pageSize);
        JobQueryObj jobQueryObj = new JobQueryObj(jobType, recruitType, keyword, sortKey, order);
        List<Position> positions = positionMapper.getPosInfoByPage(jobQueryObj);
        PageInfo<Position> info = new PageInfo<Position>(positions);
        return info;
    }


    /**
     * 根据用户的使用的id来添加对应的收藏的职务信息
     *
     * @param id
     */
    public void collect(Long userId, Long jobId) {
        JobCollection jobCollection = new JobCollection();
        jobCollection.setUserId(userId);
        jobCollection.setJobId(jobId);
        jobCollectionMapper.insertSelective(jobCollection);
    }


    /**
     * 根据userId和jobId查询用户是否收藏了该职位
     *
     * @param id
     */
    public boolean isCollected(Long userId, Long jobId) {
        return jobCollectionMapper.selectByUserIdJobId(userId, jobId);
    }


    /**
     * 根据用户的使用的id来添加对应的投递信息
     *
     * @param id
     */
    public void delivery(Long userId, Long jobId) {
        JobDelivery jobDelivery = new JobDelivery();
        jobDelivery.setUserId(userId);
        jobDelivery.setJobId(jobId);
        jobDeliveryMapper.insertSelective(jobDelivery);
    }

    /**
     * 根据id来删除收藏的职务信息
     *
     * @param id
     */
    public void delCollect(Long userId,Long jobId) {
        jobCollectionMapper.deleteByUserIdAndJobId(userId,jobId);
    }


    /**
     * 根据id信息来删除对应的职位信息
     *
     * @param id
     * @return
     */
    public int delete(Long id) {
        return positionMapper.deleteByPrimaryKey(id);
    }


    public PageInfo<Position> getMyCollection(Long userId, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Position> list = positionMapper.getMyCollection(userId);
        return new PageInfo<>(list);
    }

    public PageInfo<Position> getMyPost(Long userId, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Position> list = positionMapper.getMyPost(userId);
        return new PageInfo<>(list);
    }

    public PageInfo<Position> getMyDelivery(Long userId, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Position> list = positionMapper.getMyDelivery(userId);
        return new PageInfo<>(list);
    }
}
